#!/bin/sh

#
# This script adjusts the power control of a set of PCI devices that
# prove beneficial to enable power savings
#

PCI_DEVICES_PM_ENABLE="${PCI_DEVICES_PM_ENABLE:-true}"

set_pci_device()
{
	for dev in /sys/bus/pci/devices/* ; do
		if [ -e $dev/class -a -e $dev/power/control ]; then
			id=`basename $dev`
			case `cat $dev/class` in
			0x020000) # ethernet
				echo "Setting Ethernet device $id to $1"
				echo $1 > $dev/power/control
				;;
			0x028000) # wireless
				echo "Setting Wireless device $id to $1"
				echo $1 > $dev/power/control
				;;
			0x040300) # audio
				echo "Setting Audio device $id to $1"
				echo $1 > $dev/power/control
				;;
			0x060000) # host bridge
				echo "Setting Host Bridge $id to $1"
				echo $1 > $dev/power/control
				;;
			0x080500) # SD card reader
				echo "Setting SD card reader device $id to $1"
				echo $1 > $dev/power/control
				;;
			0x088000|0x088001) # card reader
				echo "Setting card reader device $id to $1"
				echo $1 > $dev/power/control
				;;
			0x0c0000|0x0c0010) # firewire
				echo "Setting FireWire device $id to $1"
				echo $1 > $dev/power/control
				;;
			esac
		fi
	done
}

case "$1" in
	true) # powersaving on
		[ "$PCI_DEVICES_PM_ENABLE" = true ] && set_pci_device "auto"
		;;
	false) # powersaving off
		[ "$PCI_DEVICES_PM_ENABLE" = true ] && set_pci_device "on"
		;;
	*)
		exit 254
		;;
esac

exit 0
